package org.${projectName}.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.${projectName}.dao.${pojoName}Dao;
import org.${projectName}.pojo.${pojoName};
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/${shortName}")
public class ${pojoName}Controller {
	@Autowired
	private ${pojoName}Dao ${shortName}Dao;

	public void set${pojoName}Dao(${pojoName}Dao ${shortName}Dao) {
		this.${shortName}Dao = ${shortName}Dao;
	}
	
	@RequestMapping("/all")
	public List<${pojoName}> searchAll() throws Exception {
		return ${shortName}Dao.getAll();
	}
	
	@RequestMapping("/index")
	public String index() throws Exception {
		return "${shortName}/index";
	}
	
	@RequestMapping("/list")
	@ResponseBody
	public Map<String, Object> list(@RequestParam(defaultValue="1") Integer page,@RequestParam(defaultValue="10") Integer rows,
			@RequestParam(defaultValue="asc") String order,@RequestParam(defaultValue="id") String sort,
			${pojoName} condition) throws Exception {
		Map<String, Object> map = new HashMap<>();
		
		int start = (page - 1) * rows;
		List<${pojoName}> list = ${shortName}Dao.getConditionList(start, rows, condition, sort, order);
		int total = ${shortName}Dao.getConditionCount(condition);
		
		map.put("total", total);
		map.put("rows", list);
		
		return map;
	}
	
	@RequestMapping(value="/form",method=RequestMethod.GET)
	public String add() throws Exception {
		return "${shortName}/form";
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> add(${pojoName} ${shortName}) throws Exception {
		Map<String, Object> map = new HashMap<>();
		${shortName}Dao.add(${shortName});
		map.put("result", true);
		return map;
	}
	
	@RequestMapping(value="/batchDelete")
	@ResponseBody
	public Map<String, Object> batchDelete(Integer[] ids) throws Exception {
		Map<String, Object> map = new HashMap<>();
		${shortName}Dao.batchDelete(ids);
		map.put("result", true);
		return map;
	}
	
	@RequestMapping("/view/{id}")
	@ResponseBody
	public ${pojoName} view(@PathVariable Integer id) throws Exception {
		return ${shortName}Dao.get(id);
	}
	
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> edit(${pojoName} ${shortName}) throws Exception {
		Map<String, Object> map = new HashMap<>();
		${shortName}Dao.update(${shortName});
		map.put("result", true);
		return map;
	} 
	
}
